package cn.com.twoke.application.game.mario.game.component;

import cn.com.twoke.application.game.mario.core.ecs.component.*;
import cn.com.twoke.application.game.mario.core.ecs.entity.Entity;
import lombok.Data;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;

/**
 * 玩家控制组件
 */
@Slf4j
@RequiredArgsConstructor
@Data
public class PlayerControlComponent extends InputComponent {
    /**
     * 向左移动
     */
    public final int moveLeftKey;
    /**
     * 向右移动
     */
    public final int moveRightKey;
    /**
     * 跳跃按键
     */
    public final int jumpKey;
    /**
     * 下蹲按键
     */
    public final int downKey;
    /**
     * 攻击按键
     */
    public final int attackKey;
    public boolean attacking = false;



    @Override
    public void update(Entity entity, InputState inputState, double delta) {
        try {
            if (entity.hasComponent(PlayerComponent.class) && entity.hasComponent(PositionComponent.class)) {
                PositionComponent position = entity.getComponent(PositionComponent.class);
                PlayerComponent playerComponent = entity.getComponent(PlayerComponent.class);
                JumpComponent jumpComponent = entity.getComponent(JumpComponent.class);
                RigidbodyComponent rigidbodyComponent = entity.getComponent(RigidbodyComponent.class);
                
                if (inputState.isKeyPressed(moveLeftKey)) {
                    position.x -= playerComponent.moveSpeed;
                    playerComponent.isLeft = true;
                    playerComponent.setDirection(PlayerComponent.Direction.LEFT);
                } else {
                    playerComponent.isLeft = false;
                }

                if (inputState.isKeyPressed(moveRightKey)) {
                    position.x += playerComponent.moveSpeed;
                    playerComponent.isRight = true;
                    playerComponent.setDirection(PlayerComponent.Direction.RIGHT);
                } else {
                    playerComponent.isRight = false;
                }

                if (rigidbodyComponent.isGrounded) {
                    jumpComponent.coyoteTimer = jumpComponent.coyoteTime;
                    if (playerComponent.isJump) {
                        playerComponent.isJump = false;
                        jumpComponent.isJumping = false;
                    }
                } else {
                    jumpComponent.coyoteTimer -= delta;
                }

                if (inputState.isKeyPressed(jumpKey)) {
                    if ((rigidbodyComponent.isGrounded || jumpComponent.coyoteTimer > 0) && !jumpComponent.isJumping) {
                        startJump(entity);
                    }
                    playerComponent.isJump = true;
                } else {
                    jumpComponent.isJumping = false;
                }

                if(inputState.isKeyPressed(downKey)) {
                    playerComponent.isDown = true;
                } else {
                    playerComponent.isDown = false;
                }

                if (inputState.isKeyPressed(attackKey) && !inputState.wasKeyPressed(attackKey)) {
                    // TODO 攻击
                    log.info("调用攻击了");
                    attacking = true;
                } else {
                    attacking = false;
                }
            }
        } catch (Exception e) {
            log.error("Error updating player control component for entity ID: {}", entity.getId(), e);
        }
    }

    private void startJump(Entity entity) {
        JumpComponent jump = entity.getComponent(JumpComponent.class);
        RigidbodyComponent rigidbodyComponent = entity.getComponent(RigidbodyComponent.class);

        rigidbodyComponent.velocity.y = -jump.jumpForce; // 向上为负
        jump.isJumping = true;
        jump.currentJumpTime = 0f;
        jump.coyoteTimer = 0f; // 消耗土狼时间

    }

}
